package com.yanqu.road.dao.impl.activity.chefduel;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.chefduel.ChefDuelFoodConfig;
import com.yanqu.road.entity.activity.chefduel.process2.ChefDuel2DishConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChefDuelConfigDaoImpl extends TempDao {

    public Map<Integer, ChefDuelFoodConfig> getChefDuelActivityFoodConfig(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ChefDuelFoodConfig> dataMap = new ConcurrentHashMap<>();
        String activityIdCondition = "activityId = ? ";
        String sql = "select * from t_s_activity_chefduel_food where " + activityIdCondition + "";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChefDuelFoodConfig data = iniChefDuelFoodConfig(rs);
                dataMap.put(data.getCookingOrder(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


    public Map<Integer, Map<Integer, ChefDuel2DishConfig>> getChefDuel2ActivityDishConfig(List<Integer> activityIdList) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, ChefDuel2DishConfig>> dataMap = new ConcurrentHashMap<>();
        if(activityIdList.size() == 0){
            return dataMap;
        }
        String activityIdCondition = "activityId in(" + StringUtils.listToString(activityIdList, ",") + ")";
        String sql = "select * from t_s_activity_chefduel_menu where " + activityIdCondition + "";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                ChefDuel2DishConfig data = iniChefDuel2DishConfig(rs);
                if(!dataMap.containsKey(data.getActivityId())){
                    dataMap.put(data.getActivityId(),new ConcurrentHashMap<>());
                }
                dataMap.get(data.getActivityId()).put(data.getId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer, ChefDuel2DishConfig> getChefDuel2ActivityDishConfig(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ChefDuel2DishConfig> dataMap = new ConcurrentHashMap<>();
        String activityIdCondition = "activityId = ?";
        String sql = "select * from t_s_activity_chefduel_menu where " + activityIdCondition + "";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChefDuel2DishConfig data = iniChefDuel2DishConfig(rs);
                dataMap.put(data.getId(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }


    private ChefDuelFoodConfig iniChefDuelFoodConfig(ResultSet rs) throws SQLException {
        ChefDuelFoodConfig data = new ChefDuelFoodConfig();
        data.setActivityId(rs.getInt("activityId"));
        data.setCookingId(rs.getInt("cookingId"));
        data.setCookingOrder(rs.getInt("cookingOrder"));
        data.setUnlockCondition(rs.getInt("unlockCondition"));
        data.setAddScore(rs.getInt("addScore"));
        return data;
    }

    private ChefDuel2DishConfig iniChefDuel2DishConfig(ResultSet rs) throws SQLException {
        ChefDuel2DishConfig data = new ChefDuel2DishConfig();
        data.setActivityId(rs.getInt("activityId"));
        data.setId(rs.getInt("id"));
        data.setQuality(rs.getInt("quality"));
        data.setMenu(rs.getString("menu"));
        data.setWeight(rs.getInt("weight"));
        data.setSeat(rs.getInt("seat"));
        data.setScore(rs.getInt("score"));
        return data;
    }
}
